package com.menggudf.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.menggudf.dao.PermissionDao;
import com.menggudf.entity.Permission;
import com.menggudf.model.OnePermissionModel;
import com.menggudf.model.PermissionModel;
import com.menggudf.model.ResultModel;
import com.menggudf.model.TwoPermissionModel;
import com.menggudf.service.PermissionServiceIf;

@Service
public class PermissionService implements PermissionServiceIf{

    private static final Logger LOGGER = LoggerFactory.getLogger(PermissionService.class);
    
    @Autowired
    PermissionDao PermissionDao;
    
    @Override
    @Transactional(value="medicalTM", readOnly=true)
    public ResultModel getAllPerssions() {
        List<Permission> list = PermissionDao.getAllOnePerssions();
        Map<Integer ,OnePermissionModel> onepmMap = new HashMap<Integer, OnePermissionModel>();
        for(Permission p : list){
            OnePermissionModel opm = new OnePermissionModel();
            opm.setId(p.getTwoModule());
            opm.setpId(p.getOneModule());
            opm.setName(p.getFullName());
            opm.setOpen(true);
            onepmMap.put(p.getTwoModule(), opm);
        }
        List<Permission> tList = PermissionDao.getAllTwoPerssions();
        for(Permission pp : tList){
            TwoPermissionModel tpm = new TwoPermissionModel();
            tpm.setId(pp.getTwoModule());
            tpm.setName(pp.getFullName());
            tpm.setpId(pp.getOneModule());
            OnePermissionModel opModel = onepmMap.get(pp.getOneModule());
            List<TwoPermissionModel> ttList = opModel.getChildren();
            if(ttList!=null && ttList.size()>0){
                ttList.add(tpm);
            }else{
                List<TwoPermissionModel> tt1List = new ArrayList<TwoPermissionModel>();
                tt1List.add(tpm);
                opModel.setChildren(tt1List);
            }
        }
        List<OnePermissionModel> opList = new ArrayList<OnePermissionModel>();
        for (Map.Entry<Integer,OnePermissionModel> entry : onepmMap.entrySet()){
            opList.add(entry.getValue());
        }
        return ResultModel.success("获取成功", opList);
    }

}
